package main

import (
	"fmt"
	"github.com/golang/protobuf/proto"
	rpc2 "learn/RPCandgRPC/rpc"
)

type Person struct {
	Id int32
	Name string
	Age uint32
}
var Num  []*rpc2.User_PhoneNumber
func main() {
	//创建对象
	//msg := &models.User{
	//	Id: *proto.Int32(1),
	//	Name: *proto.String("小明"),
	//	Age: *proto.Uint32(23),
	//
	//}
	phone := &rpc2.User_PhoneNumber{
		Number: *proto.String("13311112222"),
	}
	Num = append(Num, phone)
	//gender:=*rpc.User_Gender.Enum(0)
	msg := &rpc2.User{
		Id: *proto.Int32(1),
		Name: *proto.String("小明"),
		Age: *proto.Uint32(23),
		Phones: Num,
		//Gender: gender,
	}
	//序列化
	protoData, err := proto.Marshal(msg)
	if err!=nil{
		fmt.Println("Marshal错误", err)
	}
	//不能使用string
	//fmt.Printf("protoData：%v---%v---%T\n", protoData, string(protoData),protoData)

	//反序列化
	//targetType := &Person{}  //自定义不可行，不是Message类型
	targetType := &rpc2.User{}
	err = proto.Unmarshal(protoData, targetType)
	if err!=nil{
		fmt.Println("proto.Unmarshal错误：", err)
	}
	fmt.Println(targetType.Name, targetType.GetName())
	fmt.Println(targetType.Id, targetType.GetId())
	fmt.Println(targetType.Age, targetType.GetAge())
	//fmt.Println(targetType.Gender)
	fmt.Println(targetType.Phones, targetType.GetPhones())
	fmt.Println(targetType.String())

}